//
//  ScanBarcodeRecoganizedTableViewCell.swift
//  ezyform
//
//  Created by MoennigQ on 28/6/19.
//  Copyright © 2019 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit

class ScanBarcodeRecoganizedTableViewCell: UITableViewCell {
    var deleteClosure: (()->Void)?
//    let seqNoLabel = BaseLabel()
    let barcodeLabel = BaseLabel()
    let uploadedImageView = UIImageView()
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    func setUpviews(){
        self.backgroundColor = .clear
        self.selectionStyle = .none
        
        //title
        barcodeLabel.textColor = UIColor.white
        barcodeLabel.setFont(R.font.interRegular(size: 15))
        barcodeLabel.numberOfLines = 2
        barcodeLabel.adjustsFontSizeToFitWidth = true
        self.contentView.addSubview(barcodeLabel)
        barcodeLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
            make.centerY.equalToSuperview()
        }
        //uploadedImageView
        uploadedImageView.image = UIImage(named: "scanner_code_uploaded_faild")
        self.contentView.addSubview(uploadedImageView)
        uploadedImageView.snp.makeConstraints { (make) in
            make.left.equalTo(barcodeLabel.snp.right).offset(5)
            make.height.width.equalTo(16)
            make.centerY.equalToSuperview()
        }
        //deleteBtn
        let delBtn = UIButton()
        delBtn.setImage(UIImage(named: "scanner_code_delete"), for: UIControl.State.normal)
        self.contentView.addSubview(delBtn)
        delBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.height.width.equalTo(30)
            make.centerY.equalToSuperview()
        }
        delBtn.addTarget(self, action: #selector(delBtnClick), for: UIControl.Event.touchUpInside)
    }
    @objc private func delBtnClick(){
        self.deleteClosure?()
    }
    func setData(_ row: Int, list: [ScannedBarcodeModel]){
//        self.seqNoLabel.text = "\(list.count - row)."
        self.barcodeLabel.text = list[row].barcode
        //符合校验规则是白色
        let textColor = (list[row].validFlag ? UIColor.white : UIColor.red)
        barcodeLabel.textColor = textColor
        //已上传的是绿色图标
        let imageName: String = (list[row].uploadedFlag ? "scanner_code_uploaded_success" : "scanner_code_uploaded_not_uploaded")
        uploadedImageView.image =  UIImage(named: imageName)
    }
}
